public class HashBucket {
    private static class Node {
        private int key;
        private int value;
        Node next;

        public Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
    }


    private Node[]  array;
    private int size;   // 当前的数据个数
    private static final double LOAD_FACTOR = 0.75;
    private static final int DEFAULT_SIZE = 8;//默认桶的大小

    public int put(int key, int value) {
        // write code here
        //获取index值
        int index = key % array.length ;
       //遍历链表看有没有值；
        Node cur = array[index];
        while(cur != null){
            if(cur.key == key){
                cur.value = value;
                break;
            }
            cur = cur.next;
        }
        //哈希表里没有数据，用头插法插入 ；
        Node node = new Node(key , value);
        node.next = array[index];
        array[index] = node;
        size ++ ;
        //查看负载因子，如果超范围就扩容
        if(loadFactor() >=LOAD_FACTOR){
            resize();
        }
        return -1;
    }


    private void resize() {
        // write code here
           Node[] newArr = new Node[array.length * 2] ;
        for (int i = 0; i < array.length; i++) {
            Node cur = array[i];
            // 把原来的数据重新哈希即可
            while (cur != null){
                Node curNext = cur.next;
                int newIndex = cur.key % newArr.length;
                cur.next = newArr[newIndex];
                newArr[newIndex] = cur ;

                cur = curNext;
            }
        }
        this.array = newArr;
    }


    private double loadFactor() {
        return size * 1.0 / array.length;
    }


    public HashBucket() {
        this.array = new Node[DEFAULT_SIZE];
        // write code here
    }


    public int get(int key) {
        int index = key % array.length;
        Node cur = array[index];
        while (cur != null){
            if(cur.key == key){
                return cur.value;
            }
            cur = cur.next;
        }

        return  -1 ;
    }
}